
//
//  Aspect-Window.m
//  rssreader
//
//  Created by Zzz on 15/11/29.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "AppDelegate.h"
#import <XAspect/XAspect.h> // aop 面向切片
#import "MeStatus.h"
#import "LoginViewModel.h"
#import "DeviceInfo.h"

#define AtAspect Login

#define AtAspectOfClass AppDelegate
@classPatchField(AppDelegate)

AspectPatch(-, void,application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions) {
    USER_ACCOUNT_STATUS status = [[MeStatus sharedInstance] userAccountStatus];
    if(status == USER_ACCOUNT_STATUS_VERIFY){
        
        NSURL *getTokenUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@/passport/getTokenForAppLogin.json?key=123&version=1.0&timeMillis=%@",SERVICE_ADRESS,[DeviceInfo getCurDeviceTimeStr]]];
        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:getTokenUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
        NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:received options:0 error:nil];
        if([[NSString stringWithFormat:@"%@",jsondict[@"result"]] integerValue] == 1){
        NSURL *loginUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@/passport/appLoginByLoginKey.json?imie=%@&loginKey=%@&loginToken=%@",SERVICE_ADRESS,[DeviceInfo fetchGeneralDeviceId],[MeStatus sharedInstance].getLoginKey,jsondict[@"loginToken"]]];
        NSURLRequest *loginRequest = [[NSURLRequest alloc]initWithURL:loginUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
            NSData *loginRet = [NSURLConnection sendSynchronousRequest:loginRequest returningResponse:nil error:nil];
            NSDictionary *loginJsondict = [NSJSONSerialization JSONObjectWithData:loginRet options:0 error:nil];
            if([[NSString stringWithFormat:@"%@",loginJsondict[@"result"]] integerValue]==1){
                [[MeStatus sharedInstance]saveLoginKey:loginJsondict[@"loginKey"]];
                ITLogSP(@"用户已绑定手机号自动登录");
            }else{
                [[MeStatus sharedInstance] removeUserAccount];
            }
        }else{
            [[MeStatus sharedInstance] removeUserAccount];
        }
        
        
    }else{
        ITLogSP(@"请使用微信登录，并验证手机号");
    }
    XAMessageForward(application:application didFinishLaunchingWithOptions:launchOptions);
}
@end
#undef AtAspectOfClass

#undef AtAspect
